import matplotlib.pyplot as plt
import numpy as np


def gamble_game(trail):
    # 抛硬币游戏，正面是1 ， 反面是-1
    results = np.random.choice([1, -1], size=trail)
    return results


def simulate_gambling(trails):
    results = gamble_game(trails)

    # 计算累计盈亏
    cumulative_sum = np.cumsum(results)

    # 计算平均盈亏
    average_win_loss = cumulative_sum / np.arange(1, trails + 1)

    return cumulative_sum, average_win_loss


def plot_results(trails):
    cumulative_sum, average_win_loss = simulate_gambling(trails)

    # 绘制累计盈亏图
    plt.figure(figsize=(12, 6))
    plt.subplot(1, 2, 1)
    plt.plot(cumulative_sum)
    plt.title('Cumultative Win/Loss Over Trails')
    plt.xlabel('Trails')
    plt.ylabel('Cumulative Sum')

    # 平均盈亏图
    plt.subplot(1, 2, 2)
    plt.plot(average_win_loss)
    plt.axhline(y=0, color='r', linestyle='-')
    plt.title('Average Win/Loss Over Trails')
    plt.xlabel('Trails')
    plt.ylabel('Average Win/Loss')

    plt.tight_layout()
    plt.show()


plot_results(10000)
